{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](https://wikimedia.org/api/rest_v1/media/math/render/svg/5e4c7a30f3dc20c06057413aed7c89b7e1c67aeb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 封装方法tupper,接受X和Y两个参数，都限制Double类型的Tensor，返回true或false。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0], dtype=torch.uint8)"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.DoubleTensor([106])\n",
    "b = torch.DoubleTensor([9876778954322])\n",
    "def tupper(x,y):\n",
    "    assert isinstance(x,torch.DoubleTensor)\n",
    "    assert isinstance(y,torch.DoubleTensor)\n",
    "    return 1/2<torch.floor((torch.floor(y/17)*torch.pow(2,(-17*torch.floor(x)-torch.floor(y)%17)))%2)\n",
    "tupper(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0], dtype=torch.uint8)"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def tupper_numpy(x,y):\n",
    "    assert isinstance(x,torch.DoubleTensor)\n",
    "    assert isinstance(y,torch.DoubleTensor)\n",
    "    return 1/2<np.floor((np.floor(y/17)*np.power(2,(-17*np.floor(x)-np.floor(y)%17)))%2)\n",
    "tupper_numpy(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 思考：K的取值通常都很大，而Y的取值范围为k≤y≤k+16。例如‘塔珀自指公式’的K值如下。在Pytorch中是计算不了这么长的运算的。如果要计算这个数值，有没有更简单的方法？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "K=960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
